package com.cz.nongyetongv5.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils.TruncateAt;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cz.nongyetongv5.bean.TextInfo;
import com.cz.nongyetongv5.custom.wheelview.TosGallery;
import com.cz.nongyetongv5.custom.wheelview.Utils;

public class WheelViewAdapter extends BaseAdapter {

	/**
	 * 数据集
	 */
	private ArrayList<TextInfo> mData = null;
	/**
	 * 控件的宽度
	 */
	private int mWidth = ViewGroup.LayoutParams.MATCH_PARENT;

	/**
	 * 控件的高度,默认为50
	 */
	private int mHeight = 50;

	/**
	 * 上下文对象
	 */
	private Context mContext = null;

	public WheelViewAdapter(Context context) {
		mContext = context;
		// 根据密度计算高度
		mHeight = (int) Utils.pixelToDp(context, mHeight);
	}

	public void setData(ArrayList<TextInfo> data) {
		mData = data;

		// 刷新WheelView
		this.notifyDataSetChanged();
	}

	public void setItemSize(int width, int height) {
		mWidth = width;
		mHeight = (int) Utils.pixelToDp(mContext, height);
	}

	@Override
	public int getCount() {
		int count = (null != mData) ? mData.size() : 0;

		return count;
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	public int getPosition(Object item) {

		return this.mData.indexOf(item);

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		TextView textView = null;

		TextInfo info = mData.get(position);

		if (null == convertView) {
			textView = new TextView(mContext);
		} else {
			// 视图缓冲区取数据
			textView = (TextView) convertView;
		}

		// 这里设置的是sp单位
		textView.setTextSize(22);

		// // 获得的是像素值
		// float fontSize = textView.getTextSize();
		//
		// // 将像素值转换为sp
		// fontSize = Utils.px2sp(mContext, fontSize);
		//
		// // 字体大小的算法,如果文字过长则缩小
		// fontSize = fontSize - (info.mText.length() / 15) * 3f;
		//
		// // 设置为sp
		// textView.setTextSize(fontSize);

		textView.setSingleLine(true);

		textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));

		int padding = Utils.dip2px(mContext, 8);

		textView.setPadding(0, padding, 0, padding);

		textView.setTextColor(Color.parseColor("#36495C"));

		textView.setText(info.mText);
		textView.setEllipsize(TruncateAt.MARQUEE);

		return textView;
	}

}
